from django.http import JsonResponse
from django.shortcuts import render
from rest_framework.filters import OrderingFilter
from rest_framework.generics import ListAPIView, RetrieveAPIView, get_object_or_404
from rest_framework.pagination import PageNumberPagination
from rest_framework.viewsets import ModelViewSet
from disease.models import DiseaseDirectionModel, DiseaseCategoryModel, DiseaseModel, DiseaseMessageModel
from disease.serial import DiseaseDirectionSerializer, DiseaseCategorySerializer, DiseaseSerializer, \
    DiseaseMessageSerializer,MessageSerializer


# Create your views here.


# 一级分类： 方向视图
class DiseaseDirectionView(ListAPIView):
    queryset = DiseaseDirectionModel.objects.filter(is_deleted=False, is_show=True).order_by('id')
    serializer_class = DiseaseDirectionSerializer


# 二级分类： 分类视图
class DiseaseCategoryView(ListAPIView):
    # queryset = DiseaseCategoryModel.objects.all(is_deleted=False, is_show=True).order_by('-id')
    serializer_class = DiseaseCategorySerializer

    # 动态生成queryset，目的是根据方向ID查询分类数据，也就是说，如果传递了方向ID，那么就根据方向ID查询分类数据，否则就查询所有分类数据
    def get_queryset(self):
        print('--------------------')
        # self.kwargs是一个字典，包含了URL中的所有参数，包括路径参数和查询参数
        print(self.kwargs)
        # 获取方向ID, 默认值为0
        directionID = int(self.kwargs.get('directionID', 0))
        # 如果方向ID不等于0，那么就根据方向ID查询分类数据，否则就查询所有分类数据
        query_set = DiseaseCategoryModel.objects.filter(is_deleted=False, is_show=True).order_by('id')
        if directionID != 0:
            query_set = query_set.filter(direction=directionID)
        return query_set


# 三级分类： 疾病视图
class DiseaseView(ListAPIView):
    serializer_class = DiseaseSerializer

    def get_queryset(self):
        directionID = int(self.kwargs.get('directionID', 0))
        categoryID = int(self.kwargs.get('categoryID', 0))
        query_set = DiseaseModel.objects.filter(is_deleted=False, is_show=True).order_by('id')
        if directionID != 0:
            query_set = query_set.filter(direction=directionID)
        if categoryID != 0:
            query_set = query_set.filter(category=categoryID)
        return query_set


class DiseaseMessagePagination(PageNumberPagination):
    page_size = 5
    page_query_param = 'page'
    page_size_query_param = 'size'
    max_page_size = 10

# 四级分类： 疾病文章视图
class DiseaseMessageView(ListAPIView):
    serializer_class = DiseaseMessageSerializer
    filter_backends = [OrderingFilter]
    ordering_fields = ['id']
    pagination_class = DiseaseMessagePagination
    def get_queryset(self):
        directionID = int(self.kwargs.get('directionID', 0))
        categoryID = int(self.kwargs.get('categoryID', 0))
        diseaseID = int(self.kwargs.get('diseaseID', 0))
        query_set = DiseaseMessageModel.objects.filter(is_deleted=False, is_show=True).order_by('id')
        if directionID != 0:
            query_set = query_set.filter(direction=directionID)
        if categoryID != 0:
            query_set = query_set.filter(category=categoryID)
        if diseaseID != 0:
            query_set = query_set.filter(disease=diseaseID)
        return query_set

# 查看某一篇文章的内容
class DiseaseMessageDetailView(RetrieveAPIView):
    queryset = DiseaseMessageModel.objects.all()
    serializer_class = MessageSerializer
    lookup_field = 'pk'
    lookup_url_kwarg = 'messageID'


# 相关文章
class RelatedArticlesView(ListAPIView):
    serializer_class = DiseaseMessageSerializer

    def get_queryset(self):
        # Get the current article ID from URL parameters
        current_article_id = self.kwargs.get('messageID')

        # Get the current article to find its category
        current_article = get_object_or_404(DiseaseMessageModel, id=current_article_id)

        # Get related articles from the same category, excluding current article
        related_articles = DiseaseMessageModel.objects.filter(
            category=current_article.category,
            is_deleted=False,
            is_show=True
        ).exclude(id=current_article_id).order_by('-pub_date')[:4]

        return related_articles